package com.ruoyi.quartz.notify;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.mail.MailUtil;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.quartz.common.Constants;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class FollowUpAction {
    /**
     * 提醒
     */
    public static void doNotify(){
        System.out.println("发送邮件");
        sendMail(1);
    }

    /**
     * 1 发送成功邮件
     * 2 ip异常
     * 3 查询异常
     * @param type type
     */
    public static void sendMail(int type){
        switch (type){
            case 1:
                MailUtil.send("783157782@qq.com", Constants.mailTitle_1, Constants.mailContent_1, true);
                break;
            case 2:
                String cacheKey = "mail2_" + DateUtil.format(new Date(), "yyyy-MM-dd_HH");
                Object cache = CacheUtils.get(cacheKey);
                if(cache == null){
                    MailUtil.send("783157782@qq.com", Constants.mailTitle_2, "", true);
                    CacheUtils.put(cacheKey, "1");
                }
                break;
            case 3:
                MailUtil.send("783157782@qq.com", Constants.mailTitle_3, "", true);
                break;
            default:
                break;
        }
    }

    /**
     * 打开chrome
     * @param url url
     */
    public static void openLocalChrome(String url){
        try {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            TimeUnit.SECONDS.sleep(1);
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
            TimeUnit.SECONDS.sleep(1);
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

}
